package com.wisdomparking.control.app;

import java.text.DecimalFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.wisdomparking.bean.Orders;
import com.wisdomparking.bean.Parking;
import com.wisdomparking.bean.VehicleInfo;
import com.wisdomparking.config.mvc.BaseController;
import com.wisdomparking.config.mvc.Result;
import com.wisdomparking.config.mvc.ResultGenerator;
import com.wisdomparking.service.OrdersService;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.UserService;
import com.wisdomparking.service.VehicleInfoService;
import com.wisdomparking.tools.ToolDateTime;
import com.wisdomparking.tools.ToolValid;

/**
 * 订单中心
 */
@RestController
@RequestMapping("/app/orderCenter")
public class OrderController extends BaseController{
	
	@Autowired OrdersService ordersService;
	@Autowired VehicleInfoService vehicleInfoService;
	@Autowired ParkingService parkingService;
	@Autowired UserService userService;
	
	
	/**
	 * 获取用户订单
	 * 
	 * @param userId
	 * @return
	 */
	@RequestMapping("/getOrderList")
	public Result<List<Orders>> getOrderList(Integer userId, HttpServletRequest request){
		
		int pageNo = 1;
		int pageSize = 5;
		
		if(null!=request.getParameter("pageNo")){
			pageNo = Integer.parseInt(request.getParameter("pageNo"));
		}
		
		if(null!=request.getParameter("pageSize")){
			pageSize = Integer.parseInt(request.getParameter("pageSize"));
		}
		
		Map<String, Object> map = new HashMap<>();
		map.put("userId", userId);
		
		map.put("start", (pageNo-1)*pageSize);
		map.put("limit", pageSize);
		
		return ResultGenerator.genSuccessResult(ordersService.getOrdersList(map));
	}
	
	
	/**
	 * 获取订单详情
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("/getOrderDetail")
	public Result<Orders> getOrderDetail(Integer orderId){
		
		return ResultGenerator.genSuccessResult(ordersService.getOrders(orderId));
	}
	
	/**
	 * 根据车牌号获取订单详情
	 * 
	 * @param plateNumber
	 * @return
	 */
	@RequestMapping("/getOrderDetailByPlateNumber")
	public Result<Orders> getOrderDetail(String plateNumber){
		Orders orders = null;
		
		if(ToolValid.isNotBlank(plateNumber)) {
			Map<String, Object> m = new HashMap<>();
			m.put("plateNumber", plateNumber);
			m.put("statusLess", 2);
			m.put("orderByField", "id");
			m.put("orderByFlag", "desc");
			
			List<Orders> list = ordersService.getOrdersList(m);
			
			if(null!=list && list.size()>0){
				orders = list.get(0);
			}
		}
		
		return ResultGenerator.genSuccessResult(orders);
	}
	
	/**
	 * 获取订单计时
	 * 
	 * @param plateNumber
	 * @return
	 */
	@RequestMapping("/getOrderTime")
	public Result<Map<String, String>> getOrderTime(String plateNumber){
		Map<String, String> map = null;
		if(ToolValid.isNotBlank(plateNumber)) {
			map = new HashMap<>();
			Map<String, Object> m = new HashMap<>();
			m.put("plateNumber", plateNumber);
			m.put("statusLess", 2);
			m.put("orderByField", "id");
			m.put("orderByFlag", "desc");
			
			List<Orders> list = ordersService.getOrdersList(m);
			
			if(null!=list && list.size()>0){
				Orders orders = list.get(0);
				Parking parking = parkingService.getParkingByIdentification(orders.getIdentification());
				int interval = ToolDateTime.getDateMinuteSpace(orders.getStartTime(), new Date());
				
				map.put("plateNumber", plateNumber);
				map.put("interval", String.valueOf(interval));
				map.put("charging", parking.getChargingB());
			}
		}
		
		return ResultGenerator.genSuccessResult(map);
	}
	
	
	/**
	 * 获取订单计时
	 * 
	 * @param plateNumber
	 * @return
	 */
	@RequestMapping("/getOrderTimeByUserId")
	public Result<Map<String, String>> getOrderTimeByUserId(String userId){
		Map<String, String> map = null;
		
		Map<String, Object> _map = new HashMap<>();
		_map.put("userId", userId);
		List<VehicleInfo> list = vehicleInfoService.getVehicleInfoList(_map);
		
		String plateNumber = "";
		if(ToolValid.isNotEmpty(list)) {
			for(VehicleInfo v: list) {
				if(ToolValid.isNotNull(v)) {
					if(v.getIsDefault()) {
						plateNumber = v.getPlateNumber();
					}
				}
			}
		}
		
		if(ToolValid.isNotBlank(plateNumber)) {
			map = new HashMap<>();
			Map<String, Object> m = new HashMap<>();
			m.put("plateNumber", plateNumber);
			m.put("statusLess", 2);
			m.put("orderByField", "id");
			m.put("orderByFlag", "desc");
			
			List<Orders> _list = ordersService.getOrdersList(m);
			
			if(null!=_list && _list.size()>0){
				Orders orders = _list.get(0);
				Parking parking = parkingService.getParkingByIdentification(orders.getIdentification());
				int interval = ToolDateTime.getDateMinuteSpace(orders.getStartTime(), new Date());
				
				map.put("plateNumber", plateNumber);
				map.put("interval", String.valueOf(interval));
				map.put("charging", parking.getChargingB());
			}
		}
		
		return ResultGenerator.genSuccessResult(map);
	}
	
	
	
	/**
	 * 获取订单支付金额
	 * @param plateNumber
	 * @return
	 */
	@RequestMapping("/getPaymentAmount")
	public Result<Map<String, Object>> getPaymentAmount(String plateNumber){
		Map<String, Object> map = null;
		
		if(ToolValid.isNotBlank(plateNumber)) {
			Map<String, Object> m = new HashMap<>();
			m.put("plateNumber", plateNumber);
			m.put("statusLess", 2);
			m.put("orderByField", "id");
			m.put("orderByFlag", "desc");
			
			List<Orders> list = ordersService.getOrdersList(m);
			if(null!=list && list.size()>0){
				Orders orders = list.get(0);
				if(ToolValid.isNotNull(orders)){
					Parking parking = parkingService.getParking(orders.getParkingId());
					
					map = new HashMap<>();
					map.put("orderNumber", orders.getOrderNumber());
					map.put("plateNumber", plateNumber);
					int paymentAmount = 0;
					int min = ToolDateTime.getDateMinuteSpace(orders.getStartTime(), new Date());
					paymentAmount = Integer.parseInt(parking.getChargingA()) * min / 60;
					
					map.put("paymentAmount", new DecimalFormat("#.00").format(paymentAmount));
					
				}
			}
		}
		
		return ResultGenerator.genSuccessResult(map);
	}
	
	
	/**
	 * 结束计费
	 * 
	 * @param plateNumber
	 * @return
	 */
	@RequestMapping("/endCharging")
	public Result<Boolean> endCharging(String plateNumber){
		boolean flag = false;
		
		if(ToolValid.isNotBlank(plateNumber)) {
			Map<String, Object> m = new HashMap<>();
			m.put("plateNumber", plateNumber);
			m.put("status", 0);
			m.put("orderByField", "id");
			m.put("orderByFlag", "desc");
			
			List<Orders> list = ordersService.getOrdersList(m);
			if (null!=list && list.size()>0) {
				Orders orders = list.get(0);
				if (ToolValid.isNotNull(orders)) {
					Parking parking = parkingService.getParking(orders.getParkingId());
					
					int paymentAmount = 0;
					int min = ToolDateTime.getDateMinuteSpace(orders.getStartTime(), new Date());
					paymentAmount = Integer.parseInt(parking.getChargingA()) * min / 60;
					orders.setAmount(String.valueOf(paymentAmount * 100));
					orders.setStatus(1);
					
					flag = ordersService.updateOrders(orders, true);
				}
			}
		}
		
		return ResultGenerator.genSuccessResult(flag);
	}
	
	
}
